/// <reference path="../Global.js" />
/// <reference path="/Scripts/JQuery/jquery-1.4.2.js" />
/// <reference path="../../Plugins/MsgBox/JavaScript/jquery.msgBox.js" />
/// <reference path="../../Plugins/Boxy-0.1.4/JavaScripts/jQuery.boxy.js" />
/// <reference path="EmbedHandle.js" />
/// <reference path="../../../Library/UI/jquery-ui-1.8.6.custom/js/jquery-ui-1.8.6.custom.min.js" />
/// <reference path="../../../Library/UI/jquery-ui-1.8.6.custom/development-bundle/ui/jquery-ui-timepicker-addon.js" />
//#region ExaminerOrganization：试卷设置
var ExaminerOrganization = {
    Setup: {
        //#region initData：初始化数据
        initData: function (o用户操作类型, o设置类型, o试卷内容ID, o考试设置ID, o考生ID数组) {
            var dataAssembly = this.DataAssembly;
            dataAssembly.Post.uHandleType = o用户操作类型;
            dataAssembly.Post.设置类型 = o设置类型;
            dataAssembly.Post.试卷内容ID = o试卷内容ID;
            dataAssembly.Post.ID = o考试设置ID;
            if (o用户操作类型 == 1) {
                dataAssembly.Backup.操作类型 = "本地修改";
            }
            else {
                dataAssembly.Backup.操作类型 = "引用添加";
            }
            //#region 考生ID集合
            o考生ID数组 = o考生ID数组 || [];
            for (var i = 0; i < o考生ID数组.length; i++) {
                if (o考生ID数组[i].length == "36" && o考生ID数组[i] != Global.Guid.empty()) {
                    dataAssembly.Post.考生ID集合.push(o考生ID数组[i]);
                }
            }
            //#endregion
            //#region 网页已加载完成
            var elemAssembly = this.ElemAssembly;
            Global.LoadIng.ready(function () {
                elemAssembly.Post.及格条件 = $("#及格条件");
                elemAssembly.Post.考试时长 = $("#考试时长");
                elemAssembly.Post.考试开始时间 = $("#考试开始时间");
                elemAssembly.Post.考试结束时间 = $("#考试结束时间");
                elemAssembly.Post.是否公布考试结果 = $("input[type='radio'][name='是否公布考试结果']");
                elemAssembly.Other.提交按钮 = $("#提交按钮");
                elemAssembly.Post.考试开始时间
.blur(function () {
    ExaminerOrganization.Setup.ValidationData.考试开始时间();
}).change(function () {
    ExaminerOrganization.Setup.ValidationData.考试开始时间();
}).datetimepicker({
    showButtonPanel: false
});
            });
            //#endregion
        },
        //#endregion
        //#region ElemAssembly：元素集合
        ElemAssembly: {
            //#region Post：用于ajax提交服务器用到的元素对象
            Post: {
                "及格条件": 0,
                "考试时长": 0,
                "考试开始时间": 0,
                "考试结束时间": 0,
                "是否公布考试结果": 0
            },
            //#endregion
            //#region Other：其他元素对象
            Other: {
                "提交按钮": 0
            }
            //#endregion
        },
        //#endregion
        //#region DataAssembly：数据集合
        DataAssembly: {
            /* Post:异步post传输数据
            --------------- */
            Post: {
                "uHandleType": 0, /* 用户操作类型 如果组织设置 值为0 ，修改设置 值为1 */
                "设置类型": 0,
                "ID": null, /* 考试设置ID */
                "试卷内容ID": null, /* 试卷内容ID */
                "及格条件": 0,
                "考试时长": 0,
                "考试开始时间": null,
                "考试结束时间": null,
                "是否公布考试结果": true,
                "考生ID集合": []
            },
            Backup: {
                "操作类型": ""
            }
        },
        //#endregion
        //#region CheckBox：复选框
        CheckBox: {
            update考生交互总数: function () {
                var o考试ID集合 = ExaminerOrganization.Setup.DataAssembly.Post.考生ID集合 || [];
                var i考试ID长度 = o考试ID集合.length;
                $("#考生范围中选择交互总数").text(i考试ID长度);
                Global.TabSwitch.Text.set(1, "考生范围(" + i考试ID长度 + ")");
                Global.MsgBox.Hide.all();
            },
            update集合: function (o考生ID, ischecked) {
                var o考试ID集合 = ExaminerOrganization.Setup.DataAssembly.Post.考生ID集合 || [];
                for (var i = 0; i < o考试ID集合.length; i++) {
                    if (o考试ID集合[i] == o考生ID) {
                        o考试ID集合.splice(i, 1);
                    }
                }
                if (ischecked) {
                    o考试ID集合.push(o考生ID);
                }
                /* 已选择考生范围总个数 */
                this.update考生交互总数();
            },
            radioSelect: function (oself) {
                this.update集合(oself.id, oself.checked);
            },
            allSelect: function (ischecked) {
                Global.MvcPager.CheckBox.getTR().each(function (i) {
                    var o考生ID = Global.MvcPager.CheckBox.getCheckBox(this)[0].id;
                    ExaminerOrganization.Setup.CheckBox.update集合(o考生ID, ischecked);
                });
            },
            defaultSelect: function () {
                var o考试ID集合 = ExaminerOrganization.Setup.DataAssembly.Post.考生ID集合 || [];
                var d考试ID集合 = {};
                for (var i = 0; i < o考试ID集合.length; i++) {
                    d考试ID集合[o考试ID集合[i]] = true;
                }
                /* 从集合中判断是否已选择 */
                Global.MvcPager.CheckBox.getTR().each(function (i) {
                    var checkBoxElem = Global.MvcPager.CheckBox.getCheckBox(this);
                    var o考生ID = checkBoxElem[0].id;
                    if (d考试ID集合[o考生ID]) {
                        checkBoxElem.attr("checked", "checked");
                    }
                });
                /* 设置全选 */
                Global.MvcPager.CheckBox.setAll();
                /* 已选择考生范围总个数 */
                this.update考生交互总数();
            }
        },
        //#endregion
        //#region ValidationData：验证数据
        ValidationData: {
            及格条件: function (oself) {
                var regex = /[^0-9]/g;
                if (regex.exec(oself.value)) {
                    oself.value = oself.value.replace(regex, "");
                }
            },
            考试时长: function (oself) {
                var regex = /[^0-9]/g;
                if (regex.exec(oself.value)) {
                    oself.value = oself.value.replace(/[^0-9]/g, "");
                }
                this.考试开始时间();
            },
            考试开始时间: function () {
                var oSetup = ExaminerOrganization.Setup;
                var postData = oSetup.DataAssembly.Post;
                var postElem = oSetup.ElemAssembly.Post;
                /* 如果是考试设置 则更改考试开始时间与考试结束时间 */
                if (postData.设置类型 == 1) {
                    var i考试时长 = parseInt(postElem.考试时长.val().trim())
                    var startTime = postElem.考试开始时间.val().trim();
                    var date = startTime.toDate();
                    date.setMinutes(date.getMinutes() + (i考试时长 || 0));
                    this.考试结束时间(date.toText());
                }
            },
            考试结束时间: function (t结束时间) {
                $("#考试结束时间").val(t结束时间);
            }
        },
        //#endregion
        //#region Handle：多类型操作
        Handle: {
            //#region Type：操作类型
            Type: {
                "本地添加": "本地添加",
                "引用添加": "引用添加",
                "本地修改": "本地修改"
            },
            //#endregion
            //#region name：操作名称
            name: function () {
                if (ExaminerOrganization.Setup.DataAssembly.Backup.操作类型 == "本地修改") {
                    return "修改";
                }
                else {
                    return "创建";
                }
            },
            //#endregion
            //#region embedBoxyHide：隐藏嵌入式Boxy对话框
            embedBoxyHide: function () {
                if (parent && parent.ExaminerEmbedHandle) {
                    parent.ExaminerEmbedHandle.Organization.Boxy.hide();
                }
            },
            //#endregion
            embedBoxyBtn: function (isVisible) {
                /* 操作类型 */
                switch (ExaminerOrganization.Setup.DataAssembly.Backup.操作类型) {
                    case "引用添加":
                    case "本地修改":
                        if (parent && parent.ExaminerEmbedHandle) {
                            parent.ExaminerEmbedHandle.Organization.Boxy.setCloseVis(isVisible);
                        }
                        break;
                }
            },
            //#region completed：数据提交完成的回调函数
            completed: function () {
                switch (ExaminerOrganization.Setup.DataAssembly.Backup.操作类型) {
                    case "本地添加":
                        break;
                    case "引用添加":
                    case "本地修改":
                        if (parent && parent.ExaminerEmbedHandle) {
                            /* 更新数据列表 */
                            parent.ExaminerEmbedHandle.Organization.Update.list({ waitType: "正在更新" });
                        }
                        break;
                }
            }
            //#endregion
        },
        //#endregion
        //#region Format：格式化
        Format: {
            及格条件: function (fn) {
                var ownElem = ExaminerOrganization.Setup.ElemAssembly.Post.及格条件;
                var value = ownElem.val().trim();
                var pattern = /^[0-9]{1,2}$/;
                if (value == "") {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "请填写及格条件！", ownElem);
                }
                else if (!pattern.exec(value)) {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "及格条件只允许使用2位数字组合！", ownElem);
                }
                else if (parseInt(value) < 1 || parseInt(value) > 99) {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "及格条件范围在0-99之间！", ownElem);
                }
                else {
                    Global.CallBack.true_1(fn, value);
                }
            },
            考试时长: function (fn) {
                var ownElem = ExaminerOrganization.Setup.ElemAssembly.Post.考试时长;
                var value = ownElem.val().trim();
                var pattern = /^[0-9]{1,3}$/;
                if (value == "") {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "请填写考试时长！", ownElem);
                }
                else if (!pattern.exec(value)) {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "考试时长只允许使用3位数字组合！", ownElem);
                }
                else if (parseInt(value) < 5 || parseInt(value) > 300) {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "考试时长范围在5-300分钟之间！", ownElem);
                }
                else {
                    Global.CallBack.true_1(fn, value);
                }
            },
            考试开始时间: function (fn) {
                var ownElem = ExaminerOrganization.Setup.ElemAssembly.Post.考试开始时间;
                var value = ownElem.val().trim();
                var pattern = /^\d{4}(-|\/)\d{1,2}(-|\/)\d{1,2} [0-2]{0,1}\d{1}:[0-5]{0,1}[0-9]{1}$/;
                if (value == "") {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "请填写考试开始时间！", ownElem);
                }
                else if (!pattern.exec(value)) {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "考试开始时间格式错误！", ownElem);
                }
                else {
                    Global.CallBack.true_1(fn, value);
                }
            },
            考试结束时间: function (fn) {
                var ownElem = ExaminerOrganization.Setup.ElemAssembly.Post.考试结束时间;
                var value = ownElem.val().trim();
                var pattern = /^\d{4}(-|\/)\d{1,2}(-|\/)\d{1,2} [0-2]{0,1}\d{1}:[0-5]{0,1}[0-9]{1}$/;
                if (value == "") {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "请填写考试结束时间！", ownElem);
                }
                else if (!pattern.exec(value)) {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem);
                    Global.CallBack.false_3(fn, "考试结束时间格式错误！", ownElem);
                }
                else {
                    Global.CallBack.true_1(fn, value);
                }
            },
            是否公布考试结果: function (fn) {
                var ownElem = ExaminerOrganization.Setup.ElemAssembly.Post.是否公布考试结果;
                if (ownElem.eq(0).attr("checked") == false && ownElem.eq(1).attr("checked") == false) {
                    Global.TabSwitch.Trigger.click(0);
                    Global.Object.Tag.onFocus(ownElem.eq(0));
                    Global.CallBack.false_3(fn, "请选择是否公布考试结果！", ownElem);
                }
                else {
                    Global.CallBack.true_1(fn, ownElem.eq(0).attr("checked") ? true : false);
                }
            },
            考生ID集合: function (fn) {
                var o考试ID集合 = ExaminerOrganization.Setup.DataAssembly.Post.考生ID集合 || [];
                if (o考试ID集合.length == 0) {
                    Global.TabSwitch.Trigger.click(1);
                    Global.CallBack.false_3(fn, "您还没有选择考生范围！", Global.TabSwitch.getTab(1));
                }
                else {
                    Global.CallBack.true_1(fn, o考试ID集合);
                }
            }
        },
        //#endregion
        //#region postAjax：post方式提交数据到服务器
        postAjax: function (postData) {
            /* Boxy唯一键 */
            var boxyKey = "Examiner-Setup-Test";
            /* 标语 */
            var topics = Global.Boxy.Topics.Examiner.organization(this.DataAssembly.Post.设置类型, null);
            /* 用户操作类型：创建，修改，删除等 */
            var handle = this.Handle;
            /* 用户操作名称 */
            var handleName = handle.name();
            handle.embedBoxyBtn(false);
            //#region 请求等待中
            Global.Boxy.wait(boxyKey, {
                newTitle: handleName,
                dtHtml: handleName + topics,
                triggerBtn: ExaminerOrganization.Setup.ElemAssembly.Other.提交按钮
            });
            //#endregion
            /* 提交修改数据 */
            Global.AjaxServer.Post.json("/Organization/SetupUCEditor/", postData, function (fData) {
                /* 数据修改成功 */
                if (fData.result) {
                    Global.Boxy.success(boxyKey, {
                        newTitle: handleName + "成功",
                        dtHtml: topics + "已" + handleName + "成功.",
                        boxyOptions: {
                            beforeHide: function () {
                                handle.embedBoxyHide();
                            },
                            afterHide: function () {
                                handle.completed();
                            }
                        }
                    });
                }
                /* 数据修改失败 */
                else {
                    handle.embedBoxyBtn(true);
                    Global.Boxy.failure(boxyKey, {
                        newTitle: handleName + "失败",
                        dtHtml: topics + handleName + "失败",
                        newMessage: fData.info
                    });
                }
            });
        },
        //#endregion
        //#region submit：提交数据
        submit: function () {
            var o设置类型 = ExaminerOrganization.Setup.DataAssembly.Post.设置类型;
            var o验证列表 = o设置类型 == 0 ? ["及格条件", "考试时长", "考生ID集合"] : ["及格条件", "考试时长", "考试开始时间", "考试结束时间", "是否公布考试结果", "考生ID集合"];
            Global.MsgBox.submit(this, o验证列表);
        }
        //#endregion
    }
};